package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApAuthMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class ApAuthServiceImpl extends ServiceImpl<ApAuthMapper,ApUserRealname> implements ApAuthService {

    @Autowired
    private ApAuthMapper apAuthMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private IWemediaClient wemediaClient;

    @Override
    public ResponseResult findList(AuthDto dto) {

        //分页参数检查
        dto.checkParam();

        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = Wrappers.<ApUserRealname>lambdaQuery();

        if(dto.getStatus() != null){
            lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        lambdaQueryWrapper.orderByDesc(ApUserRealname::getCreatedTime);
        IPage page = new Page(dto.getPage(), dto.getSize());
        page = page(page, lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;

    }

    @Override
    public ResponseResult authFail(AuthDto dto) {
        //审核失败直接改状态
        if(dto == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer realnameId = updateRealnameStatus(dto,ApUserRealname.Status.FAIL.getCode());
        return ResponseResult.okResult(realnameId);
    }

    //修改状态
    private Integer updateRealnameStatus(AuthDto dto, Short status) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        if(StringUtils.isNotBlank(dto.getMsg())){
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealname.setStatus(status);
        apAuthMapper.updateById(apUserRealname);
        return apUserRealname.getId();
    }

    @Override
    public ResponseResult authPass(AuthDto dto) {
        //审核失败直接改状态
        if(dto == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        Integer realnameId = updateRealnameStatus(dto,ApUserRealname.Status.PUBLISHED.getCode());

        //创建自媒体用户
        createWmUser(realnameId);

        return ResponseResult.okResult(realnameId);
    }

    private ResponseResult createWmUser(Integer realnameId) {

        ApUserRealname apUserRealname = apAuthMapper.selectById(realnameId);

        if(apUserRealname == null){
            return ResponseResult.okResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        Integer userId = apUserRealname.getUserId();
        ApUser apUser = apUserMapper.selectById(userId);
        String name = apUser.getName();

        WmUser wmUser = wemediaClient.findUserByName(name);

        if(wmUser == null){
            wmUser= new WmUser();
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setPhone(apUser.getPhone());
            wmUser.setStatus(9);
            wemediaClient.wmUserSave(wmUser);
        }
        apUser.setFlag((short) 1);
        apUserMapper.updateById(apUser);

        return null;

    }


}
